package com.ruoyi.project.naming.namingop.service.impl;

import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.VersionUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.naming.namingop.domain.NamingOp;
import com.ruoyi.project.naming.namingop.mapper.NamingOpMapper;
import com.ruoyi.project.naming.namingop.service.INamingOpService;
import net.ipip.ipdb.CityInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 起名上报Service业务层处理
 *
 * @author shixiangwen
 * @date 2022-04-21
 */
@Service
@DataSource(DataSourceType.DICT)
public class NamingOpServiceImpl implements INamingOpService {

    @Autowired
    private NamingOpMapper namingOpMapper;

    /**
     * 查询起名上报
     *
     * @param id 起名上报ID
     * @return 起名上报
     */
    @Override
    public NamingOp selectNamingOpById(Long id) {
        NamingOp namingOp = namingOpMapper.selectNamingOpById(id);
        SetOtherInfo(namingOp);
        return namingOp;
    }

    /**
     * 查询起名上报列表
     *
     * @param namingOp 起名上报
     * @return 起名上报
     */
    @Override
    public List<NamingOp> selectNamingOpList(NamingOp namingOp) {
        List<NamingOp> list = namingOpMapper.selectNamingOpList(namingOp);
        list.forEach(item -> {
            SetOtherInfo(item);
        });
        return list;
    }

    /**
     * 新增起名上报
     *
     * @param namingOp 起名上报
     * @return 结果
     */
    @Override
    public int insertNamingOp(NamingOp namingOp) {
        return namingOpMapper.insertNamingOp(namingOp);
    }

    /**
     * 修改起名上报
     *
     * @param namingOp 起名上报
     * @return 结果
     */
    @Override
    public int updateNamingOp(NamingOp namingOp) {
        return namingOpMapper.updateNamingOp(namingOp);
    }

    /**
     * 删除起名上报对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNamingOpByIds(String ids) {
        return namingOpMapper.deleteNamingOpByIds(Convert.toStrArray(ids));
    }

    private void SetOtherInfo(NamingOp namingOp) {
        CityInfo cityInfo = IpUtils.getCityInfo(namingOp.getIp());
        if (cityInfo != null) {
            namingOp.setIpInfo(cityInfo.getRegionName() + " " + cityInfo.getCityName());
        }
        String version = VersionUtil.getVersion(namingOp.getUa());
        namingOp.setClientVersion(version);
    }

    /**
     * 删除起名上报信息
     *
     * @param id 起名上报ID
     * @return 结果
     */
    @Override
    public int deleteNamingOpById(Long id) {
        return namingOpMapper.deleteNamingOpById(id);
    }
}
